package p366;/**
 * @Author：ming
 * @Date：2023/10/17 9:41 PM
 * @Filename：Lc2904
 */

/**
 * @author ming
 */
public class Lc2904 {
    public String shortestBeautifulSubstring(String s, int k) {
        int len = s.length();
        int l =0;
        int r =0;
        int count=0;
        int minLen=len;
        String min = new String();
        while(r<len){
            if(s.charAt(r)=='1'){
                count++;
            }

            while (count>=k){
                if(r-l<=minLen){
                    if(r-l<minLen){
                        minLen = r-l;
                        min = s.substring(l,r+1);
                    }else {
                        String a = s.substring(l,r+1);
                        min = min.compareTo(a)>0?a:min;
                    }


                }
                if(s.charAt(l)=='1'){
                    count--;
                }
                l++;
            }
            r++;
        }

        return min;

    }


}
